package com.example.demo.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONUtil;

import java.util.Objects;

public class BaseController {
    public Object ok() {
        Dict dict = Dict.create().set("code", 200).set("msg", "OK");
        return JSONUtil.toJsonStr(dict);
    }

    public Object error() {
        Dict dict = Dict.create().set("code", -1).set("msg", "ERROR");
        return JSONUtil.toJsonStr(dict);
    }

    public Object ok(Object obj) {
        Dict dict = Dict.create()
                .set("code", 200)
                .set("msg", "OK")
                .set("obj", obj);
        return JSONUtil.toJsonStr(dict);

    }

    protected Integer page2Limit(Integer page) {
        if (Objects.isNull(page)) {
            page = 1;
        }
        return (Math.max(1, page) - 1) * 10;
    }

    protected Integer page2Limit(Integer page, Integer pageSize) {
        if (Objects.isNull(page)) {
            page = 1;
        }
        return (Math.max(1, page) - 1) * pageSize;
    }
}
